﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HUST.JackRiver.Demo.Doc2Text
{
    public class TypeDetector
    {
        string filePath;

        public TypeDetector(string FilePath)
        {
            filePath = FilePath;
        }
        private bool is_doc()
        {
            bool result = false;
            Microsoft.Office.Interop.Word.ApplicationClass app = null;
            Microsoft.Office.Interop.Word.Document doc = null;
            try
            {
                object missing = System.Reflection.Missing.Value;
                object FileName = filePath;
                object readOnly = true;
                object isVisible = false;


                app = new Microsoft.Office.Interop.Word.ApplicationClass();
                doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,
                    ref missing, ref missing, ref missing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                app.Quit();
            }
            return result;
        }

        private bool is_ppt()
        {
            bool result=false;
            Microsoft.Office.Interop.PowerPoint.ApplicationClass app = null;
            Microsoft.Office.Interop.PowerPoint.Presentation presentation = null;
            try
            {

                app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
                presentation = app.Presentations.Open(filePath, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                app.Quit();
            }
            return result;
        }

        private bool is_xls()
        {
            bool result=false;
            Microsoft.Office.Interop.Excel.ApplicationClass app = null;
            Microsoft.Office.Interop.Excel.Workbook doc = null;
            try
            {
                object missing = System.Reflection.Missing.Value;
                object readOnly = true;

                app = new Microsoft.Office.Interop.Excel.ApplicationClass();
                doc = app.Workbooks.Open(filePath, missing, readOnly, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                app.Quit();
            }
            return result;
        }


        private bool is_pdf()
        {
            bool result = false;
            try
            {
                org.pdfbox.pdmodel.PDDocument doc = org.pdfbox.pdmodel.PDDocument.load(filePath);
                result = true;
            }
            catch
            {
                result = false;
            }
            return result;
        }


        public string Detect()
        {
            string type = "unknown";
            if (is_pdf())
            {
                type = "pdf";
            }
            else
            {
                if (is_doc())
                {
                    type = "doc";
                }
                else
                {
                    if (is_ppt())
                    {
                        type = "ppt";
                    }
                    else
                    {
                        if (is_xls())
                        {
                            type="xls";
                        }
                    }
                }
            }
            return type;
        }
    }
}
